//
//  KWTTopTipView.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/10/24.
//

#import "KWTTopTipView.h"
@interface KWTTopTipView()

@property (nonatomic, strong) QMUILabel *deviceIdLab;

@end
@implementation KWTTopTipView

-(instancetype)init
{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

-(void)showWithDeviceId:(NSString*)deviceId
{
    self.deviceIdLab.text = [NSString stringWithFormat:@"ID:%@",deviceId];
    [kApplication.keyWindow addSubview:self];
    self.frame = kAppWindow.bounds;
}

-(void)setupUI
{
    
    self.backgroundColor = kCOLOR_HEX_A(@"000000", 0.4);
    UIView *contentView = kView;
    contentView.userInteractionEnabled = YES;
    contentView.backgroundColor = UIColor.whiteColor;
    [contentView rounded:8];
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(35);
        make.right.mas_offset(-35);
        make.centerY.mas_offset(0);
        make.height.offset(75);
    }];
    
    UIView *bgView = kView;
    bgView.backgroundColor = CFTabViewBgColor;
    [bgView rounded:8];
    [contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"deviceId_tip_icon"]];
    [bgView addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(28);
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
    }];
    
    _deviceIdLab = [QMUILabel labelWithFont:kSYSTEMFONT(15) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8) textAligment:NSTextAlignmentLeft];
    _deviceIdLab.adjustsFontSizeToFitWidth = YES;
    _deviceIdLab.minimumScaleFactor = 0.8;
    [bgView addSubview:_deviceIdLab];
    [_deviceIdLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(icon.mas_right).offset(10);
        make.centerY.mas_offset(0);
        make.right.mas_offset(-5);
    }];
    
    QMUIButton *closeBtn = [[QMUIButton alloc]init];
    closeBtn.hidden = YES;
    [closeBtn setImage:[UIImage imageNamed:@"deviceId_tip_close"] forState:UIControlStateNormal];
    [self addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(50);
        make.top.equalTo(contentView.mas_bottom).offset(35);
        make.centerX.mas_offset(0);
    }];
    
    kWeakSelf;
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf tapAction];
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [self addGestureRecognizer:tap];
    
    
}

-(void)tapAction
{
    if (self.closeBlock) {
        self.closeBlock();
    }
    [self removeFromSuperview];
}

@end
